/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

/**
 * PageViewAdapter
 */
public class PageViewAdapter extends PageSliderProvider {
    private List<AbstractPageView> pageViews;

    public PageViewAdapter(List<AbstractPageView> list) {
        if (list != null) {
            pageViews = list;
            return;
        }
        pageViews = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return this.pageViews.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i1) {
        if (i1 >= 0 && i1 < this.pageViews.size()) {
            Component comPt = pageViews.get(i1).getRootView();
            componentContainer.addComponent(comPt);
            return comPt;
        }

        return null;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i1, Object o1) {
        if (componentContainer == null) {

        } else if (o1 instanceof Component) {
            componentContainer.removeComponent((Component) o1);
        } else {

        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o1) {
        return false;
    }

    @Override
    public String getPageTitle(int position) {
        return this.pageViews.get(position).getName();
    }
}